var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    parentId:0,
    contractName:"",
    showAddDialog:false,
    contractSubTypeName:"",
    companyContractList:[],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '文件类型', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad(options) {
    this.setData({
      parentId:parseInt(options.id),
      contractName:options.name
    });
    requestClient = new ZYRequestClient();
  },
  // 加载文件小类
  loadCompanySubContractTypeList(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany');
    if(defaultCompany == ''){
      defaultCompany = { id:0 };
    }
    requestClient.getCompanySubContractTypeList(_this.data.parentId,defaultCompany.id,function(res){
      _this.setData({
        companyContractList:res.data.data
      });
    });
  },
  onShow(){
    this.loadCompanySubContractTypeList();
  },
  // 显示添加对话框
  bindShowAddClick:function(e){
    this.setData({
      showAddDialog:true,
      contractSubTypeName:''
    });
  },
  // 输入框
  formInputChange(e){
    this.setData({
      contractSubTypeName:e.detail.value
    });
  },
  // 添加文件类型
  bindAddContractType:function(e){
    var _this = this;
    _this.setData({
      showAddDialog:false
    });
    // 判断是否取消
    if(e.detail.index == 0){
      return;
    }
    if(_this.data.contractSubTypeName.trim() == ''){
      wx.showToast({
        title: '文件类型不能空!',
        icon:"error",
        mask:true
      })
      return;
    }

    var defaultCompany = wx.getStorageSync('defaultCompany');
    var contract = {
      name:_this.data.contractSubTypeName,
      parentId:_this.data.parentId,
      companyId:defaultCompany.id
    };
    requestClient.addCompanyContractType(contract,function(res){
      _this.loadCompanySubContractTypeList();
    })
  },
  // 操作菜单
  bindContractClick(e){
    var _this = this;
    var contract = e.currentTarget.dataset.contract;
    if(contract.type == 'common'){
      return;
    }
    wx.showActionSheet({
      itemList: ['删除'],
      itemColor:'#ff0000',
      success(e){
        if(e.tapIndex == 0){
          wx.showModal({
            title:'删除文件类型',
            content:'您确定要删除'+contract.name+'的文件类型吗',
            success(r){
              if(r.confirm){
                requestClient.deleteCompanyContractType(contract.id,function(res){
                  _this.loadCompanySubContractTypeList();
                })
              }
            }
          })
        }
      }
    })
  }
})